<<<<<<< HEAD
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
=======
class Student{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Main {
    public static void main1(String[] args) {
        //使用常量串构造
        String str = "hello";
        //直接newString对象
        String str2 = new String("world");
        //使用字符数组进行构造
        char[] array = {'h','e','l','l','o'};
        String str3 = new String(array);

        System.out.println(str);
        System.out.println(str2);
        System.out.println(array);
        System.out.println(str3);
        System.out.println(str3.length());

        String str4 = "";
        System.out.println("输出：" + str4); //输出： 没有东西
        System.out.println(str4.length());
        System.out.println(str4.isEmpty());


        String str5 = null;
        //System.out.println(str5.isEmpty());
        //会出现空指针异常，因为是null，指向空

        System.out.println("hello".length());
    }

    public static void main2(String[] args) {
        String str1 = new String("hello");
        String str2 = new String("hello");
        //比较两个字符串是否相等，若通过这种方法，其实是比较地址了，所以两个字符串必然不相等
        System.out.println(str1 == str2);       //false
        System.out.println(str1.equals(str2)); // true   equals被String重写了

        String str3 = "hello";
        String str4 = "hello";
        System.out.println(str3 == str4);     //true
        System.out.println(str3.equals(str4)); //true
        //两个都是true，涉及到字符串常量池，存储在堆上，直接""相当于直接赋值
    }

    public static void main3(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("aello");
        //s1和s2比较 大小，s1 > s2返回大于0的数字，s1 < s2返回小于0的数字，否则返回0
        //返回值对应ascll码的差值
        System.out.println(s1.compareTo(s2));
        //总结：比较大小时，对应字符不一样，就看对应字符的ASCLL码值
        //对应字符长度不一样
    }

    public static void main4(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("Hello");
        System.out.println(s1.equals(s2));  //false
        //忽略大小写,比较字符串
        System.out.println(s1.equalsIgnoreCase(s2)); //true
    }

    public static void main5(String[] args) {
        //字符串查找
        String s1 = new String("hello");
        //返回字符串对应下标的字符
        char ch = s1.charAt(1);  //下标不能越界，返回字符串对应下标的字符
        System.out.println(ch); //得到e

        //返回对应字符出现的下标位置
        //int index = s1.indexOf('l');

        //从指定位置开始找某个字符出现的位置
        int index = s1.indexOf('l',3);
        System.out.println(index); //1 //3

        //字符串查找 从一个字符串找到另外一个字符串
        int index2 = s1.indexOf("llo");
        System.out.println(index2); //2
        //方法的重载

        //从后往前找
        int index3 = s1.lastIndexOf("l");
        System.out.println(index3);   //3
        int index4 = s1.lastIndexOf("l",1);
        System.out.println(index4);   //-1

    }


    public static void main6(String[] args) {
        //转换
        //字符串和整数转换
        String str1 = String.valueOf(123);
        System.out.println(str1);
        String str2 = String.valueOf(new Student("zhangsan",11));
        System.out.println(str2); //输出Student@3b07d329
        //对toString进行重写，序列化和反序列化

        //包装类：首先是一个类，其次类中有更多的方法，使用起来很方便
        int a = Integer.parseInt("123");
        System.out.println(a); //123
        double b = Double.parseDouble("12.34");
        System.out.println(b); //12.34
    }


    public static void main7(String[] args) {
        //大小写转换
        String str = "hello";
        String ret = str.toUpperCase();
        System.out.println(ret); //HELLO
        System.out.println(str); //hello
        //并不是在原本的基础上进行修改，是产生新的对象  源码：return new.....
        //字符串的改变都是产生新的对象

        String str2 = "HELLO";
        String ret2 = str2.toLowerCase();
        System.out.println(ret2); //hello

    }

    public static void main8(String[] args) {
        //字符串转化为数组
        String str1 = "hello";
        char[] chars = str1.toCharArray();
        for (char ch : chars) {
            System.out.println(ch);
            //h e l l o
        }

        //格式化输出
        String s = String.format("%d-%d-%d",2024,3,27);
        System.out.println(s);  //2024-3-27
    }

    public static void main9(String[] args) {
        //字符串的替换
        String str1 = "aaaabbbbbbccccc";
        String ret1 = str1.replace('a','p');
        System.out.println(ret1);  //ppppbbbbbbccccc

        //可以替换一组字符
        String ret2 = str1.replace("ab","sssss");
        System.out.println(ret2);  //aaasssssbbbbbccccc

        String ret3 = str1.replaceFirst("aa","qqqq");
        System.out.println(ret3);  //qqqqaabbbbbbccccc

    }

    public static void main(String[] args) {
        //字符串拆分
        String str1 = "name=zhangsan&age=10";
        String[] ret1 = str1.split("&");
        for (int i = 0; i < ret1.length; i++) {
            System.out.println(ret1[i] + " ");
        }
        //name=zhangsan
        //age=10

        String str2 = "hello world hello ely";
        //随机分成两组
        //hello
        //world hello ely
        String[] ret2 = str2.split(" ",2);
        //假如limit：12 ，无法分12，但是能保证分割四次，不够就算了
        for (int i = 0; i < ret2.length; i++) {
            System.out.println(ret2[i] + " ");
        }

        //很特殊！！！！IP地址的分割
        String str3 = "192.168.1.1";
        //正则表达式的分割
//        String[] ret3 = str3.split(".");
//        for (int i = 0; i < ret3.length; i++) {
//            System.out.println(ret3[i] + " ");
//        }  //啥都不输出
        String[] ret3 = str3.split("\\.");
        for (int i = 0; i < ret3.length; i++) {
            System.out.println(ret3[i] + " ");
        }
        //192
        //168
        //1
        //1

        //字符"|","*","+" 都得加上转义字符，前面加上"\\"
        //而如果是"\",那么就要写成"\\\\"
        //如果太多，可以用"|"作为连字符
        String str4 = "name=zhangsan&age=10";
        String[] ret4 = str1.split("=|&");
        for (int i = 0; i < ret4.length; i++) {
            System.out.println(ret4[i] + " ");
        }
        //name
        //zhangsan
        //age
        //10
>>>>>>> cca01c8c1cb48334ddcd07f5002db7a049190867
    }
}